import logging
from logging.handlers import RotatingFileHandler
import os
from datetime import datetime

import Config

pid = os.getpid()
os.makedirs(Config.LOG_PATH, exist_ok=True)

def configure_logger():
    logger = logging.getLogger("zxgk_api_proxy")
    logger.setLevel(logging.INFO)

    if logger.hasHandlers():
        logger.handlers.clear()

    # 手动按日期命名
    today = datetime.now().strftime("%Y-%m-%d")
    log_file = os.path.join(Config.LOG_PATH, f"app_{pid}_{today}.log")

    handler = RotatingFileHandler(
        log_file,
        maxBytes=1024 * 1024 * 5,  # 5MB
        backupCount=30
    )
    handler.setLevel(logging.INFO)

    formatter = logging.Formatter(
        '%(asctime)s - %(name)s - %(levelname)s - [%(process)d] %(message)s'
    )
    handler.setFormatter(formatter)

    logger.addHandler(handler)

    return logger